package vue;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JPanel;

import controleur.Controleur;

public class PanelBackground extends JPanel {
	// Variable(s) statique(s)
	private static final long serialVersionUID = 1L;
	
	// Variable(s) d'instance
	private BufferedImage image = null;
	private BufferedImage image2 = null;
	private BufferedImage balle = null;
	private final int height;
	private final int width;
	
	// Constructeur(s)
	public PanelBackground() {
		this.setLayout(null);
		Dimension dimension = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
		this.height =(int)dimension.getHeight();
		this.width =(int)dimension.getWidth();
		this.setPreferredSize(new Dimension(1000, 1000));
		File fiche = new File("images/background.png");
		File f2 = new File("./images/crosshair.png");
		File fBalle = new File("./images/ammo.png");
		try {
			this.image=ImageIO.read(fiche);
			ImageIO.write(this.image, "png", fiche);
			
			this.image2=ImageIO.read(f2);
			ImageIO.write(this.image2, "png", f2);
			
			this.balle=ImageIO.read(fBalle);
			ImageIO.write(this.balle, "png", fBalle);
		} catch (IOException e) {}
	}
	
	// Methode(s)
	@Override
	public void paintComponent(Graphics g){
		Graphics2D g2 = (Graphics2D)g;
		g2.drawImage(this.image, 0, 0, this.width, this.height, null);
		if(Controleur.getInstance() != null) {
			g2.drawImage(this.image2, Controleur.getInstance().getX(), Controleur.getInstance().getY(), 46, 46, null);
		} else {
			g2.drawImage(this.image2, 0, 0, 46, 46, null);
		}
		if(Controleur.getInstanceCanard()!=null && Controleur.getInstanceCanard().getCanards().size()!=0) {
			for(int i = 0; i < Controleur.getInstanceCanard().getCanards().get(0).getCoupsRestants(); i++) {
				g2.drawImage(this.balle, i*19+10, this.height-70, 19, 64, null);
			}
		}
		g.setFont(new Font("DuckHUnt", Font.BOLD, 50));
		g.setColor(Color.WHITE);
		g.drawString("Score :"+Controleur.getInstance().getScore()+" Ratio : "+Controleur.getInstance().getRatio(), this.width-550, this.height-30);
	}
	
	public void addDuck(Duck canard) {
		this.add(canard);
	}
}
